package com.self.service.handle;

import com.self.service.domain.Result;
import com.self.service.enums.ResultEnum;
import com.self.service.exception.ConstantException;
import com.self.service.util.ResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class ExceptionHandle {

    private static final Logger LOGGER = LoggerFactory.getLogger(ExceptionHandle.class);

    @ResponseBody
    @ExceptionHandler
    public Result handle(Exception exception) {

        if (exception instanceof ConstantException) {
            ConstantException constantException = (ConstantException) exception;
            return ResultUtil.error(constantException.getCode(), constantException.getLocalizedMessage());
        } else {
            LOGGER.error("【系统异常】", exception);
            return ResultUtil.error(ResultEnum.SYSTEM_ERROR);
        }
    }


}
